package com.ohuang.jiaxiaotong.fragment;

import android.app.Dialog;
import android.content.Intent;
import android.os.Bundle;

import androidx.appcompat.app.AlertDialog;
import androidx.fragment.app.Fragment;

import android.util.DisplayMetrics;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.Window;
import android.view.WindowManager;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;

import com.allen.library.SuperTextView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.bumptech.glide.request.RequestOptions;
import com.ohuang.jiaxiaotong.Activity.ChangePawActivity;
import com.ohuang.jiaxiaotong.Activity.CollectActivity;
import com.ohuang.jiaxiaotong.Activity.UploadIconActivity;
import com.ohuang.jiaxiaotong.Activity.fillUserMsg2Activity;
import com.ohuang.jiaxiaotong.Activity.fillUserMsgActivity;
import com.ohuang.jiaxiaotong.R;
import com.ohuang.jiaxiaotong.Util.EventMsgCode;
import com.ohuang.jiaxiaotong.Util.Ohuang;
import com.ohuang.jiaxiaotong.Util.Sys;
import com.ohuang.jiaxiaotong.entity.EventMsg;
import com.ohuang.jiaxiaotong.entity.User;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.io.IOException;


public class MyFragment extends Fragment {
    private View view;
    private User user0;
    private ImageView img_icon;
    private TextView textView_username, textView_role;
    private SuperTextView superTextView_collect, superTextView_changePsd, superTextView_edit;
    private Button button_exit;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        EventBus.getDefault().register(this);//注册EventBus
    }

    @Override
    public View onCreateView(LayoutInflater inflater, ViewGroup container,
                             Bundle savedInstanceState) {
        // Inflate the layout for this fragment
        view = inflater.inflate(R.layout.fragment_my, container, false);
        initView();
        initListener();
        return view;
    }

    private void initListener() {
        img_icon.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if (Sys.isClick()) {
                    getContext().startActivity(new Intent(getContext(), UploadIconActivity.class)//打开上传头像界面
                    );
                }
            }
        });
        button_exit.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                exitdialogshow();//弹出退出登录对话框
            }
        });

        superTextView_changePsd.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClickListener(SuperTextView superTextView) {
                Intent intent = new Intent(getContext(), ChangePawActivity.class);
                intent.putExtra("username", user0.getUsername());
                startActivity(intent);
            }
        });
        superTextView_edit.setOnSuperTextViewClickListener(new SuperTextView.OnSuperTextViewClickListener() {
            @Override
            public void onClickListener(SuperTextView superTextView) {
                if (Sys.isClick()) {
                    if ("1".equals(user0.getRole())) {
                        getContext().startActivity(new Intent(getContext(), fillUserMsg2Activity.class));//打开家长个人信息界面
                    } else {
                        getContext().startActivity(new Intent(getContext(), fillUserMsgActivity.class));//打开家长个人信息界面
                    }
                }
            }
        });

        superTextView_collect.setOnSuperTextViewClickListener(s->{
           if (Sys.isClick()){
               getContext().startActivity(new Intent(getContext(), CollectActivity.class));
           }
        });
    }

    private void initView() {
        img_icon = view.findViewById(R.id.img_icon_my);
        textView_username = view.findViewById(R.id.txt_username_my);
        textView_role = view.findViewById(R.id.txt_role_my);
        superTextView_changePsd = view.findViewById(R.id.superTxt_changePsd_my);
        superTextView_collect = view.findViewById(R.id.superTxt_collect_my);
        superTextView_edit = view.findViewById(R.id.superTxt_edit_my);
        button_exit = view.findViewById(R.id.btn_exit_my);
        if (user0 != null) {//加载头像
            Glide.with(getContext()).load(user0.getUrl())
                    .error(R.drawable.img_icon)
                    .fallback(R.drawable.img_icon)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(img_icon);

            textView_username.setText("用户名：" + user0.getUsername());
            if ("1".equals(user0.getRole())) {
                textView_role.setText("身份：家长");
            } else {
                textView_role.setText("身份：老师");
            }
        }
    }

    //退出登录对话框
    private void exitdialogshow() {
        View view = LayoutInflater.from(getContext()).inflate(R.layout.dialog_exit, null);
        final Dialog dialog = new AlertDialog.Builder(getContext(), R.style.MyDialog).setView(view).create();

        Button quxiao = view.findViewById(R.id.dialog_btn_exit_no);
        Button queding = view.findViewById(R.id.dialog_btn_exit_ok);

        quxiao.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                dialog.dismiss();
            }
        });
        queding.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                try {
                    dialog.dismiss();
                    Ohuang.writefile(getContext(), "username.txt", "");
                    Ohuang.writefile(getContext(), "password.txt", "");
                    getActivity().finish();
                } catch (IOException e) {
                    e.printStackTrace();
                }
            }
        });
        dialog.show();
        //需要先显示再设置大小
        Window window = dialog.getWindow();
        WindowManager.LayoutParams params = window.getAttributes();
        DisplayMetrics dm = getResources().getDisplayMetrics();//获取屏幕分辨率
        int screenWidth = dm.widthPixels;
        int screenHeight = dm.heightPixels;
        params.width = (int) (0.7 * screenWidth);
        window.setAttributes(params);
    }

    @Override
    public void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }

    //eventBus获取User
    @Subscribe(threadMode = ThreadMode.MAIN, sticky = true)
    public void GetUserEventBus(User user) {
        user0 = user;
        if (img_icon != null) {
            Glide.with(getContext()).load(user.getUrl())
                    .error(R.drawable.img_icon)
                    .fallback(R.drawable.img_icon)
                    .apply(RequestOptions.bitmapTransform(new CircleCrop()))
                    .into(img_icon);
        }
    }
}
